package cn.codexin.wechatofficial.service;

import cn.codexin.wechatofficial.mapper.HTDB.dbo.CommonMapper;
import cn.codexin.wechatofficial.mapper.HTDB.dbo.HistoricalMapper;
import cn.codexin.wechatofficial.mapper.HTHIS.dbo.HistoricalMapper2;
import cn.codexin.wechatofficial.model.dbo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ly
 * @date 2020/8/14 0014 1:01
 */
@Service
public class HistoricalService {


    @Autowired
    private HistoricalMapper historicalMapper;

    @Autowired
    private HistoricalMapper2 historicalMapper2;

    @Autowired
    private TimeService timeService;

    //查看所有的遥测点
    public List<MVDefPojo> allMVDot(int ModuleID){
        List<MVDefPojo> mvDefPojoList = historicalMapper.allMVDots(ModuleID);
        return mvDefPojoList;
    }
    //查看所有的遥信点
    public List<SPSDefPojo> allSPSDots(int ModuleID){
        List<SPSDefPojo> spsDefPojoList = historicalMapper.allSPSDots(ModuleID);
        SPSDefPojo allSpsDefPojo = new SPSDefPojo();
        // 添加所有点的选项
        allSpsDefPojo.setDotID(-1);
        allSpsDefPojo.setDotName("全部遥信点");
        spsDefPojoList.add(0,allSpsDefPojo);
        return spsDefPojoList;
    }
    //查看所有的告警点
    public List<AlarmDefPojo> allAlarmDots(int ModuleID){
        List<AlarmDefPojo> alarmDefPojoList1 = historicalMapper.allAlarmDots(ModuleID,"MVDef");
        for(int i = 0;i<alarmDefPojoList1.size();i++){
            AlarmDefPojo pojo1 = alarmDefPojoList1.get(i);
            pojo1.setDotType(3);
        }
        List<AlarmDefPojo> alarmDefPojoList2 = historicalMapper.allAlarmDots(ModuleID,"SPSDef");
        for(int i = 0;i<alarmDefPojoList2.size();i++){
            AlarmDefPojo pojo2 = alarmDefPojoList2.get(i);
            pojo2.setDotType(1);
        }
        List<AlarmDefPojo> alarmDefPojoList = alarmDefPojoList1;
        alarmDefPojoList.addAll(alarmDefPojoList2);
        alarmDefPojoList.add(0,new AlarmDefPojo().setDotID(-1).setDotName("所有告警点"));
        return alarmDefPojoList;

    }
    /*查询遥测值列表*/
    public List<MVValuePojo> telemetry(int DotID,long startTime,long endTime){

        List<MVValuePojo> mvValuePojoList = new ArrayList<>();

        Float InvalidFloat = historicalMapper.MVInvalidByID(DotID);
        String lastTable = timeService.lastTime("MVValue",endTime);
        String currentTable = timeService.currentTime("MVValue",endTime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<MVValuePojo> mvValuePojoList1 = historicalMapper2.telemetry(DotID,startTime,endTime,lastTable);
        for(int i = 0;i<mvValuePojoList1.size();i++){
            if (InvalidFloat != null && mvValuePojoList1.get(i).getValue() == InvalidFloat)
                continue;
            MVValuePojo pojo = mvValuePojoList1.get(i);
            pojo.setTime(sdf.format(pojo.getSampleTime()));
            mvValuePojoList.add(pojo);
        }
        List<MVValuePojo> mvValuePojoList2 = historicalMapper2.telemetry(DotID,startTime,endTime,currentTable);
        for(int i = 0;i<mvValuePojoList2.size();i++){
            if (InvalidFloat != null && mvValuePojoList2.get(i).getValue() == InvalidFloat)
                continue;
            MVValuePojo pojo = mvValuePojoList2.get(i);
            pojo.setTime(sdf.format(pojo.getSampleTime()));
            mvValuePojoList.add(pojo);
        }
        return mvValuePojoList;
    }

    /*查询遥信值列表*/
    public List<SPSValuePojo> variantRecord(int DotID,int ModuleID,long startTime,long endTime){
        List<SPSValuePojo> list = new ArrayList<>();
        if (DotID == -1){
            for (SPSDefPojo allSPSDot : allSPSDots(ModuleID)) {
                if (allSPSDot.getDotID() != -1){
                    List<SPSValuePojo> spsValuePojos = variantRecordDeal(allSPSDot.getDotID(), startTime, endTime);
                    if (spsValuePojos != null)
                        list.addAll(spsValuePojos);
                }
            }
        }else {
            list = variantRecordDeal(DotID,startTime,endTime);
        }
        return list;
    }

    /*处理遥信值列表*/
    public List<SPSValuePojo> variantRecordDeal (int DotID,long startTime,long endTime){
        String lastTable = timeService.lastTime("SPSValue",endTime);
        String currentTable = timeService.currentTime("SPSValue",endTime);
        List<SPSValuePojo> spsValuePojoList1 = historicalMapper2.variantRecord(DotID,startTime,endTime,lastTable);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i = 0;i<spsValuePojoList1.size();i++){
            SPSValuePojo pojo = spsValuePojoList1.get(i);
            pojo.setTime(sdf.format(pojo.getSampleTime()));
            pojo.setDotID(DotID);
        }
        List<SPSValuePojo> spsValuePojoList2 = historicalMapper2.variantRecord(DotID,startTime,endTime,currentTable);
        for(int i = 0;i<spsValuePojoList2.size();i++){
            SPSValuePojo pojo = spsValuePojoList2.get(i);
            pojo.setTime(sdf.format(pojo.getSampleTime()));
            pojo.setDotID(DotID);
        }
        List<SPSValuePojo> spsValuePojoList = spsValuePojoList1;
        spsValuePojoList.addAll(spsValuePojoList2);
        return spsValuePojoList;
    }
    /*查询告警值列表*/
    public List<AlarmValuePojo> warningRecord(int DotID,int ModuleID,int DotType,long startTime,long endTime){
        List<AlarmValuePojo> alarmValuePojos = new ArrayList<>();
        if (DotID == -1){
            List<AlarmDefPojo> list = allAlarmDots(ModuleID);
            for (AlarmDefPojo pojo : list) {
                if (pojo.getDotID() != -1) {
                    List<AlarmValuePojo> pojos = warningRecordDeal(pojo.getDotID(), pojo.getDotType(), startTime, endTime);
                    if (pojos != null)
                        alarmValuePojos.addAll(pojos);
                }
            }
        }else {
            alarmValuePojos = warningRecordDeal(DotID,DotType,startTime,endTime);
        }

        return alarmValuePojos;
    }
    /*查询告警值列表处理*/
    public List<AlarmValuePojo> warningRecordDeal(int DotID,int DotType,long startTime,long endTime) {
        List<AlarmValuePojo> alarmValuePojoList = historicalMapper2.warningRecord(DotID, DotType, startTime, endTime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < alarmValuePojoList.size(); i++) {
            AlarmValuePojo pojo = alarmValuePojoList.get(i);
            pojo.setTime(sdf.format(pojo.getOccurTime())).setDotID(DotID);
        }
        return alarmValuePojoList;
    }
}
